﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wedn.BLL.Common;
using Wedn.BLL.Data;
using Wedn.Model.Data;

namespace Wedn.Interface.Template
{
    /// <summary>
    /// 自定义母版页可以选择从此类派生, 从而获得数据 在需要调用数据时直接调用方法名(例如:"<%=GetTitle()%>")
    /// 如果使用系统默认的CodeBehind技术, 则必须在方法名前面加上类名. (例如:"<%=(MasterFun.GetTitle())%>")
    /// </summary>
    public class MasterFun : System.Web.UI.MasterPage
    {
        /// <summary>
        /// 获取跟站点有关信息数据, 返回查询到的字符串, 如果不存在则返回null
        /// </summary>
        /// <param name="parameter">指定查选的名称, 参照说明</param>
        /// <returns>查询到的字符串, 如果不存在则返回null</returns>
        public string SiteInfo(string parameter)
        {
            string value = string.Empty;
            string key = parameter.Trim().ToLower();
            switch (key)
            {
                case "sitetitle":
                    value = OptionsManager.GetSetting("title") + "|" + OptionsManager.GetSetting("subtitle");
                    break;
                case "siteurl":
                    value = ConfigHelper.SiteUrl;
                    break;
                case "postscount":
                    value = PostsManager.GetPostsCount().ToString();
                    break;
                case "commentscount":
                    value = CommentsManager.GetCommentsCount().ToString();
                    break;
                case "tagscount":
                    value = TermsManager.GetTermsCount("tag").ToString();
                    break;
                case "categoryscount":
                    value = TermsManager.GetTermsCount("category").ToString();
                    break;
                default:
                    value = OptionsManager.GetSetting(key);
                    break;
            }
            return value;
        }

        /// <summary>
        /// 计算页面加载时间
        /// </summary>
        /// <param name="dt">开始时间,传入(index.starttick)即可</param>
        /// <returns></returns>
        public string GetLoadTime(DateTime dt)
        {
            return Convert.ToString(DateTime.Now.Subtract(dt).TotalMilliseconds / 1000);
        }

        public bool IsPost
        {
            get { return Wedn.BLL.Common.RequestHelper.QueryString("type", true) == "post"; }
        }


        /// <summary>
        /// 加载导航栏
        /// </summary>
        /// <returns>导航栏HTML块</returns>
        public string LoadNav()
        {
            StringBuilder sbd = new StringBuilder("<ul id=\"nav\" class=\"menu\">");
            List<Link> list = LinksManager.GetLinks("nav", true);
            foreach (var item in list)
            {
                if (item.ParentId == 0)
                {
                    sbd.Append(string.Format("<li><a href=\"{0}\" title=\"{1}\" target=\"{2}\">{3}</a>", item.Url, item.Title, item.Target, item.Name));
                    LoadNav(sbd, item);
                }
            }
            sbd.Append("</ul>");
            return sbd.ToString();
        }

        /// <summary>
        /// 递归加载子菜单
        /// </summary>
        /// <param name="sbd"></param>
        /// <param name="item"></param>
        private void LoadNav(StringBuilder sbd, Link item)
        {
            List<Link> subList = LinksManager.GetLinks(item.Id, true);
            if (subList.Count != 0)
            {
                sbd.Append("<ul style=\"display: none;\">");
                foreach (var subItem in subList)
                {
                    sbd.Append(string.Format("<li><a href=\"{0}\" title=\"{1}\" target=\"{2}\">{3}</a>", subItem.Url, subItem.Title, subItem.Target, subItem.Name));
                    LoadNav(sbd, subItem);
                }
                sbd.Append("</ul>");
            }
            sbd.Append("</li>");
        }
    }
}
